<?php

namespace App\Library\Service;

use App\Library\Logic\OperateLogLogic;
use App\Library\Logic\ShopOrderLogic;
use App\Library\Logic\ShopOrderReturnLogic;
use App\Library\Requests\Base\SerialNumberRequest;
use App\Library\Requests\Logistic\LogisticEditRequest;
use App\Library\Requests\Logistic\LogisticSendRequest;
use App\Library\Service\Base\BaseLogisticService;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\JsonResponse;

class LogisticService extends BaseLogisticService
{

    /**
     * 物流查询
     * @param SerialNumberRequest $request
     * @return JsonResponse
     *
     * @throws GuzzleException
     */
    public function logisticInfo(SerialNumberRequest $request): JsonResponse
    {
        $model = $this->logic->findBySerialNumber($request);
        $logisticInfo = [];

        if (is_null($model)) return fail(433);
        else if ($model->object_type === 0 && (int)$model->type === 0) $logisticInfo = self::auctionOrderLogistics($model);
        else if ($model->object_type) {
            if ((int)$model->type === 0){ $logisticInfo = self::orderLogistics($model);
            } else if ((int)$model->type === 1){$logisticInfo = self::orderReturnLogistics($model);}
        }

        if ($logisticInfo instanceof JsonResponse) return $logisticInfo;
        else return success($logisticInfo);
    }

    public function edit(LogisticEditRequest $request): JsonResponse
    {
        $model = $this->logic->findBySerialNumber($request);
        if (is_null($model)) return fail(433);
        $model->express_no = $request->expressNo;
        $model->save();
        OperateLogLogic::add();
        return success();
    }

    /**
     *
     * @param LogisticSendRequest $request
     * @return JsonResponse
     *
     */
    public function send(LogisticSendRequest $request): JsonResponse
    {
        //object_type:订单类型;0:auction;1:product;
        //type;0订单1售后
        if ($request->objectType === 1) {
            if ($request->type === 0) {
                $order = ShopOrderLogic::findByWhere('order_id', $request->objectId);
                if (is_null($order)) return fail(433);
                $order->order_buy_status = 4;
                $order->order_sell_status = 4;
                $order->order_delivery_time = time();
                $order->save();
                $order->ext->status = 3;
                $order->ext->status_deliver = 1;
                $order->ext->deliver_at = now();
                $order->ext->save();
            } else {
                $orderReturn = ShopOrderReturnLogic::findByWhere('orId', $request->objectId);
                if (is_null($orderReturn)) return fail(433);
                else if ((int)$orderReturn->or_type !== 1) return failMessage('退货类型错误');
                else if ((int)$orderReturn->or_sell_status !== 2) return failMessage('请耐心等待审核通过');
                $orderReturn->or_buy_status = 4;
                $orderReturn->or_sell_status = 4;
                $orderReturn->save();
            }
        }
        $this->logic->insertForModel(handleRequest($request));
        return success();
    }
}
